<?php

namespace App\Http\Controllers\Shop\Product;

use App\Http\Controllers\ShopController;
use App\Models\Business\Business as BusinessModel;
use App\Models\Business\Collection as CollectionModel;
use App\Models\Product\Product as ProductModel;
use Illuminate\Support\Facades\Validator;

class ProductController extends ShopController
{
    public function index()
    {
        $page = request('page',1);
        $limit = request('limit',10);
        $typeid = request('typeid',0);
        $keyword = request('keyword','');
        $flag = request('flag',0);
        $orderBy = request('orderBy','createtime');

        // 封装条件数组
        $where = [
            'status' => '1'
        ];

        if($typeid)
        {
            $where['typeid'] = $typeid;
        }

        if($keyword)
        {
            // $where['name'] = ['like','%关键字%']
            $where[] = ['name','LIKE',"%$keyword%"];
        }

        if($flag)
        {
            $where['flag'] = $flag;
        }

        $start = ($page - 1) * $limit;

        $ProductData = ProductModel::where($where)->orderBy($orderBy,'desc')->offset($start)->limit($limit)->get();

        $ProductCount = ProductModel::where($where)->count();

        $data = [
            'count' => $ProductCount,
            'list' => $ProductData
        ];

        if($ProductData)
        {
            return $this->success('查询成功',$data);
        }else{
            return $this->error('暂无商品',[]);
        }

    }

    public function info()
    {
        $proid = request('proid',0);
        $busid = request('busid',0);

        $Product = ProductModel::where(['status' => '1','id' => $proid])->first();

        if(!$Product)
        {
            return $this->error('商品不存在');
        }

        // 判断登录用户是否收藏
        $Product['collection'] = false;

        $business = BusinessModel::find($busid);

        if($business)
        {
            $collection = CollectionModel::where(['busid' => $busid,'proid' => $proid])->first();

            $Product['collection'] = $collection ? true : false;
        }

        return $this->success('查询成功',$Product);
    }

    public function collection()
    {
        $proid = request('proid',0);
        $busid = request('busid',0);

        $where = [
            'proid' => $proid,
            'busid' => $busid,
        ];

        $collection = CollectionModel::where($where)->first();
    
        $msg = '未知信息';

        if($collection)
        {
            // 已收藏
            $msg = '取消收藏';

            $result = $collection->delete();
        }else{
            // 未收藏
            $data = [
                'busid' => $busid,
                'proid' => $proid
            ];

            $validate = [
                [
                    'busid'  => 'required',
                    'proid'   => 'required'
                ],
                [
                    'busid.required' => '未知用户',
                    'proid.required'     => '未知商品'
                ]
            ];
    
            $validator = Validator::make($data,...$validate);
    
            if($validator->fails())
            {
                return $this->error($validator->errors()->first());
            }

            $msg = '收藏';

            $result = CollectionModel::create($data);
        }

        if($result === false)
        {
            return $this->error("{$msg}失败");
        }else{
            return $this->success("{$msg}成功");
        }
    
    }
}
